<template>
  <div>
    <el-button size="mini" type="success" @click="addFormAlet">添加</el-button>
    <el-table :data="speciallyList" stripe style="width: 100%">
      <el-table-column prop="nid" label="专业编号"></el-table-column>
      <el-table-column prop="name" label="专业名称"></el-table-column>
      <el-table-column prop="superior.name" label="所属系别"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="checkInfo(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div :class="['alertBg',isShow?'show':'']">
      <el-form class="alertForm" label-width="80px">
        <el-form-item label="专业名称">
          <el-input v-model="submitForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属系别">
          <el-select v-model="collegeOpetion.value" placeholder="请选择" @change="cOptionChange">
            <el-option
              v-for="item in collegeOpetion.list"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="formSubmit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { handleMessage } from "../../plugins";
export default {
  data() {
    return {
      collegeOpetion: { value: "", list: [] },
      submitForm: {
        name: "",
        superior: ""
      },
      speciallyList: [],
      isShow: false,
      isEdit: false,
      editId: ""
    };
  },
  methods: {
    async formSubmit() {
      //添加
      if (!this.submitForm.name) {
        this.$message({ type: "warning", message: "请填写专业名称" });
        return;
      }
      if (!this.submitForm.superior) {
        this.$message("请选择所属系别");
        this.$message({ type: "warning", message: "请选择所属系别" });
        return;
      }
      if (!this.isEdit) {
        let res = await this.$http.post("rest/Specialty", this.submitForm);
        handleMessage(this, res.data);
      } else {
        let res = await this.$http.put(
          `rest/Specialty/${this.editId}`,
          this.submitForm
        );
        handleMessage(this, res.data);
      }
      this.close();
      this.getSpeciallyList();
    },
    checkInfo(index, row) {
      //点击编辑弹出框
      this.isEdit = true;
      this.isShow = true;
      this.editId = row._id;
      this.submitForm = {
        name: row.name,
        superior: row.superior
      };
      this.collegeOpetion.value = row.superior.name;
    },
    async handleDelete(index, row) {
      //点击删除
      this.$confirm(`是否确定要删除 "${row.name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const res = await this.$http.delete(`rest/Specialty/${row._id}`);
        handleMessage(this,res.data);
        this.getSpeciallyList();
      });
    },
    async getCList() {
      //获得系别列表
      let res = await this.$http.get("Department");
      this.collegeOpetion.list = res.data;
    },
    async getSpeciallyList() {
      //获得专业列表
      let res = await this.$http.get("rest/Specialty");
      this.speciallyList = res.data;
    },
    cOptionChange(e) {
      //当下拉框改变时候
      this.submitForm.superior = e;
    },
    close() {
      //关闭
      this.isShow = false;
      this.submitForm = {};
      this.collegeOpetion.value = "";
    },
    addFormAlet() {
      //点击添加按钮弹框
      this.isShow = true;
    }
  },
  created() {
    this.getCList();
    this.getSpeciallyList();
  }
};
</script>

<style scoped>
.alertBg {
  background: rgba(0, 0, 0, 0.4);
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: center;
  display: none;
}
.show {
  display: flex;
}
</style>